package edu.stu.mall.repository

import android.content.Context
import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import edu.stu.mall.repository.data.RecommendedProductData
import edu.mall.http.BaseResponse
import edu.mall.http.RetrofitClient
import edu.stu.mall.repository.data.BannerListData
import edu.stu.mall.repository.data.CartListData
import edu.stu.mall.repository.data.LoginData
import edu.stu.mall.repository.data.ProductCategoryListData
import edu.stu.mall.repository.data.ProductCategoryListDataItem
import edu.stu.mall.repository.data.ProductDetailData
import edu.stu.mall.repository.data.UserInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference


object Repository {
    private const val SUCCESS_CODE = 0
    private const val NEED_LOGIN_CODE = -1001

    //    静态类不能用上下文，可能会导致内存泄漏的风险
    //  private var context:Context?=null
    private var mContext: WeakReference<Context>? = null
    fun init(context: Context) {
        mContext = WeakReference(context)
    }

    /*
    * 获取首页数据
    * */

    //    suspend fun getHomeList(pageCount: String): HomeListData? {
//        val data: BaseResponse<HomeListData?>? =
//            RetrofitClient.getInstance().getDefault(ApiService::class.java).homeList(pageCount)
//        if (data?.getData()!=null){
//            return data.getData()
//        }
//
//        return null
//    }
    suspend fun getCartList(): CartListData? {
        val data: BaseResponse<CartListData?>? =
            RetrofitClient.getInstance().getDefault(ApiService::class.java).cartList()

        if (data?.getData() != null) {
            return data.getData()
        } else {

        }
        return null
    }

    suspend fun postLogin(): LoginData? {
        val data: BaseResponse<LoginData?>? =
            RetrofitClient.getInstance().getDefault(ApiService::class.java).login()
        if (data?.getData() != null) {
            return data.getData()
        }
        return null
    }

    /*
    *
    * */
    suspend fun getRecommendedProductList(): RecommendedProductData? {
        val data: BaseResponse<RecommendedProductData?>? =
            RetrofitClient.getInstance().getDefault(ApiService::class.java).recommendedProductList()
        if (data?.getData() != null) {
            return data.getData()
        }
        return null
    }

    /**
     * 首页banner
     */
    suspend fun getMallBanner(): BannerListData? {
        val data: BaseResponse<BannerListData>? =
            RetrofitClient.getInstance().getDefault(ApiService::class.java).mallBanner()
        if (data?.getData() != null) {
            return data.getData()
        }
        return null
    }

    suspend fun getProductCategory(): ProductCategoryListData? {
        val data: BaseResponse<ProductCategoryListData>? =
            RetrofitClient.getInstance().getDefault(ApiService::class.java).productCategory()
        if (data?.getData() != null) {
            return data.getData()

        }
        return null
    }

    /**
     * 商品详情
     */
    suspend fun getProductDetail(pid: String): ProductDetailData? {
        val data: BaseResponse<ProductDetailData?>? =
            RetrofitClient.getInstance().getDefault(ApiService::class.java).productDetail(pid = pid)
        if (data?.getData() != null) {
            return data.getData()
        }
        return null
    }

    /**
     * 个人信息
     */
    suspend fun getUserInfo(): UserInfo? {
        val data: BaseResponse<UserInfo>? =
            RetrofitClient.getInstance().getDefault(ApiService::class.java).userInfo()
        val errCode = data?.getErrCode()
        val datas = data?.getData()
        Log.e("TAG", "aaaaaaaaaaaaaaa: $errCode")
        Log.e("TAG", "aaaaaaaaaaaaaaa: $datas")
        if (data?.getData() != null) {
            return data.getData()
        }
        return null
    }

    private fun <T> responseCall(response: BaseResponse<T?>?): T? {
        if (response == null) {
            GlobalScope.launch(Dispatchers.Main) {
                showToast("请求异常")

            }
            return null

        }
        //请求正常，返回数据
        if (response.getErrCode() == SUCCESS_CODE) {
            return response.getData()


        } else if (response.getErrCode() == NEED_LOGIN_CODE) {
//            startToLogin()
            return null
        } else {

            showToast(response.getErrMsg())
            return null
        }
    }


    private fun showToast(msg: String?) {
        GlobalScope.launch(Dispatchers.Main) {
            ToastUtils.showShort(msg ?: "请求异常")
        }
    }
}